using System;
using System.Collections.Generic;
using UnityEngine;

class NetMsgHandler : Singleton<NetMsgHandler>
{
    private Dictionary<NetMsgID, Action<byte[]>> handlerMap;

    protected override void AutoInit()
    {
        handlerMap = new Dictionary<NetMsgID, Action<byte[]>>();
        var commonHandler = new CommonHandler();
        var stateHandler = new StateHandler();
    }

    public void RegisterReq(NetMsgID msgId, Action<byte[]> handler)
    {
        if (handlerMap.ContainsKey(msgId))
        {
            Debug.LogWarning($"{msgId}重复注册");
            return;
        }
        handlerMap.Add(msgId, handler);
    }

    public void HandleResp(NetMsgID msgId, byte[] buff)
    {
        if (!handlerMap.ContainsKey(msgId))
        {
            Debug.LogWarning($"未注册的消息：{msgId}");
            return;
        }
        GameManager.Instance.RunInMainThread(() =>
        {
            handlerMap[msgId].Invoke(buff);
        });
    }
}